package studio.raptor.databus.graph.it;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import studio.raptor.databus.graph.domain.Event;
import studio.raptor.databus.graph.rule.original.CsipC2uEvents;

/**
 * @author jack
 * @since 1.0
 */
public class AutoPrepareTestingEnv {

  protected static Connection connection;

  public static List<Event> events = Lists.newArrayList();

  public static Map<String, CsipC2uEvents> csipC2uEvents = Maps.newHashMap();
  public static Map<String, Map<String, List<Long>>> csipC2uParams = Maps.newHashMap();


  @BeforeClass
  public static void setup() throws SQLException, IOException {
    H2DatabaseHelper h2DatabaseHelper = new H2DatabaseHelper("~", "usb ", "root", "root", 2);
    h2DatabaseHelper.prepareH2Database();

    connection = h2DatabaseHelper.tryToGetConnection();
    connection.setAutoCommit(false);
    buildEvents();

  }

  @AfterClass
  public static void after() throws SQLException {
    if (null != connection) {
      connection.close();
    }
  }


  private static void buildEvents() throws SQLException {
    String query_event = "select * from csip_c2u_events_new order by event_id";
    String query_params = "select * from csip_c2u_paras_new where event_id = ?";

    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(query_event);
    while (rs.next()) {
      Event event = new Event();
      event.setCode("1000");
      CsipC2uEvents csipC2uEvent = new CsipC2uEvents();
      csipC2uEvent.setEventId(rs.getString("event_id"));
      event.setEventId(rs.getString("event_id"));
      event.setAreaId(rs.getInt("area_id"));
      event.setName("billing");

      csipC2uEvent.setPartyId(rs.getLong("party_id"));
      event.setPartyId(rs.getLong("party_id"));
      event.setOrderId(rs.getLong("object_key"));
      event.setOrderId(rs.getLong("object_key"));

      csipC2uEvent.setProdId(rs.getLong("prod_id"));
      event.setProdId(rs.getLong("prod_id"));
      event.setStatus(rs.getString("event_status"));
      csipC2uEvent.setOrderTypeId(rs.getString("order_type_id"));
      event.setOrderTypeId(rs.getString("order_type_id"));

      PreparedStatement pstmt = connection.prepareStatement(query_params);
      pstmt.setString(1, event.getEventId());

      ResultSet parasRs = pstmt.executeQuery();
      List<Long> acctIds = new ArrayList<>();
      List<Long> offers = new ArrayList<>();
      List<Long> offer2ProdIds = new ArrayList<>();
      Map<String, List<Long>> paras = Maps.newHashMap();

      while (parasRs.next()) {
        String name = parasRs.getString("para_name");
        if ("OFFERS".equals(name)) {
          event.addOffer(parasRs.getLong("para_value"));

          offers.add(parasRs.getLong("para_value"));
        } else if ("OFFERS2PRODID".equals(name)) {
          event.addOfferProdId(parasRs.getLong("para_value"));

          offer2ProdIds.add(parasRs.getLong("para_value"));
        } else if ("ACCOUNT_ID".equals(name)) {
          event.addAccountId(parasRs.getLong("para_value"));

          acctIds.add(parasRs.getLong("para_value"));
        }
        paras.put("OFFERS", offers);
        paras.put("OFFERS2PRODID", offer2ProdIds);
        paras.put("ACCOUNTS", acctIds);
      }

      csipC2uParams.put(event.getEventId(), paras);
      events.add(event);
      csipC2uEvents.put(event.getEventId(), csipC2uEvent);

      pstmt.close();

    }
    stmt.close();

  }

}
